package mn.slide.dailynews;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import mn.slide.dailynews.model.City;
import mn.slide.dailynews.tv.TvListActivity;
import mn.slide.dailynews.utils.HttpConnection;
import mn.slide.horoscope.HoroScopeTabActivity;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener
{
	private ArrayList<City> weatherList;
	private City city;
	private RelativeLayout weather;
	private String LOG_TAG = getClass().getSimpleName();
	private Button zodiacImg;
	private Button tvButton;
	private ImageView weatherState;
	private TextView currentTemp;
	private TextView cityName;
	private TextView dayTemp;
	private int[] weatherImage = { R.drawable.sunny, R.drawable.cloudy, R.drawable.mostly_cloudy, R.drawable.rainy };
	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		context = this;
		zodiacImg = (Button)findViewById(R.id.zodiacImg);
		zodiacImg.setOnClickListener(this);
		weather = (RelativeLayout) findViewById(R.id.weather);
		weather.setOnClickListener(this);
		tvButton = (Button) findViewById(R.id.tvButton);
		tvButton.setOnClickListener(this);
		
		weatherState = (ImageView) findViewById(R.id.wheaterImage);
		currentTemp = (TextView) findViewById(R.id.tempLabel);
		cityName = (TextView) findViewById(R.id.cityLabel);
		dayTemp = (TextView) findViewById(R.id.dayTemp);

		downloadXmlFile("http://192.168.1.121:8888/dailynews/weather.xml");		// http://192.168.1.121:8888/dailynews/weather.xml
	}

	private void downloadXmlFile(String url)
	{
		Handler handler = new Handler()
		{
			public void handleMessage(Message message)
			{
				switch (message.what)
				{
					case HttpConnection.DID_START:
					{
						break;
					}
					case HttpConnection.DID_SUCCEED:
					{
						new updateWeather().execute();
						break;
					}
					case HttpConnection.DID_ERROR:
					{
						break;
					}
				}

			}
		};
		new HttpConnection(handler).get(url);
	}

	private class updateWeather extends AsyncTask<Void, Void, Void>
	{
		protected void onPreExecute()
		{

		}

		@Override
		protected void onPostExecute(Void result)
		{
			for (City c : weatherList)
			{
				if (c.name.equalsIgnoreCase("Улаанбаатар"))
				{
					weatherState.setImageResource(weatherImage[c.state]);
					currentTemp.setText(c.currentTemp);
					cityName.setText(c.name);
					dayTemp.setText(c.dayTemp + " / " + c.nightTemp);
				}
			}
		}

		@Override
		protected Void doInBackground(Void... params)
		{
			updateWeather(HttpConnection.inputStream);
			return null;
		}
	}

	private void updateWeather(InputStream is)
	{
		weatherList = new ArrayList<City>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			DefaultHandler handler = new DefaultHandler()
			{
				String name;
				String date;
				int state;
				String current;
				String day;
				String night;

				public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
				{
					if (qName.equalsIgnoreCase("city"))
					{
						name = attributes.getValue("name");
						date = attributes.getValue("date");
						state = Integer.parseInt(attributes.getValue("state"));
					}
					if (qName.equalsIgnoreCase("temp"))
					{
						current = attributes.getValue("current");
						day = attributes.getValue("day");
						night = attributes.getValue("night");
					}
				}

				public void endElement(String uri, String localName, String qName) throws SAXException
				{
					if (qName.equalsIgnoreCase("city"))
					{
						Log.i(LOG_TAG, " ");
						Log.i(LOG_TAG, "name: [" + name + "]");
						Log.i(LOG_TAG, "date: [" + date + "]");
						Log.i(LOG_TAG, "state: [" + state + "]");
						Log.i(LOG_TAG, "current: [" + current + "]");
						Log.i(LOG_TAG, "day: [" + day + "]");
						Log.i(LOG_TAG, "night: [" + night + "]");

						city = new City(name, date, state, current, day, night);
						weatherList.add(city);
					}
				}

			};
			saxParser.parse(is, handler);

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void onClick(View v)
	{
		if (v == weather)
		{
			// startActivity(new Intent(MainActivity.this,
			// WeatherActivity.class));
		}
		if(zodiacImg==v){
			startActivity(new Intent(MainActivity.this, HoroScopeTabActivity.class));
		}
		if(tvButton==v){
			startActivity(new Intent(MainActivity.this, TvListActivity.class));
		}
	}
}